﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NutriStat;
using NutriStat.Data;

namespace NutriStat
{
    /// <summary>
    /// The form used for importing a non-NutriStat table
    /// </summary>
    public partial class ImportTableDialog : NutriDialog
    {
        #region Data
        #region Protected Data
        #endregion Protected Data

        #region Private Data
        bool empty;
        string inputFileName;
        string outputFileName;
        private FormMainMenu menu;        
        private NutriPatient inputPatient;
        private NutriPatient outputPatient;
        private ImportConfiguration importConfig;
        #endregion Private Data

        #region Internal Data
        #endregion Internal Data
        #endregion Data

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public ImportTableDialog(FormMainMenu mainMenu) : base()
        {
            InitializeComponent();
            menu = mainMenu;
            outputPatient = new NutriPatient();
            inputPatient = new NutriPatient();
            Empty = true;
            importConfig = new ImportConfiguration();
        }
        #endregion Constructors

        #region Events
        /// <summary>
        /// Handles the Click event for the the cancel button.
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event for the the link button.
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonLink_Click(object sender, EventArgs e)
        {
            if (listBoxOutputs.SelectedIndex == -1 || listBoxInputs.SelectedIndex == -1) 
            {
                return;
            }
            string sourceField = string.Empty;
            string fieldType = string.Empty;
            string units = string.Empty;

            sourceField = listBoxInputs.SelectedItem.ToString();
            fieldType = listBoxOutputs.SelectedItem.ToString();

            if(listBoxOutputs.SelectedItem.ToString().Contains("Sex"))
            {
                ImportFormatGenderDialog importFormatGenderDialog = new ImportFormatGenderDialog();
                importFormatGenderDialog.ShowDialog(this);
                units = importFormatGenderDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Height"))
            {
                ImportFormatHeightDialog importFormatHeightDialog = new ImportFormatHeightDialog();
                importFormatHeightDialog.ShowDialog(this);
                units = importFormatHeightDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Weight"))
            {
                ImportFormatWeightDialog importFormatWeightDialog = new ImportFormatWeightDialog();
                importFormatWeightDialog.ShowDialog(this);
                units = importFormatWeightDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Recumbent"))
            {
                ImportFormatYesNoDialog importFormatYesNoDialog = new ImportFormatYesNoDialog();
                importFormatYesNoDialog.ShowDialog(this);
                units = importFormatYesNoDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Edema"))
            {
                ImportFormatYesNoDialog importFormatYesNoDialog = new ImportFormatYesNoDialog();
                importFormatYesNoDialog.ShowDialog(this);
                units = importFormatYesNoDialog.Result;                
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Age"))
            {
                ImportFormatTimeDialog importFormatTimeDialog = new ImportFormatTimeDialog();
                importFormatTimeDialog.ShowDialog(this);
                units = importFormatTimeDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Head circumference"))
            {
                ImportFormatHeightDialog importFormatHeadDialog = new ImportFormatHeightDialog();
                importFormatHeadDialog.ShowDialog(this);
                units = importFormatHeadDialog.Result;
            }

            else if (listBoxOutputs.SelectedItem.ToString().Contains("Arm circumference"))
            {
                ImportFormatHeightDialog importFormatArmDialog = new ImportFormatHeightDialog();
                importFormatArmDialog.ShowDialog(this);
                units = importFormatArmDialog.Result;
            }


            dataGridViewLinkedFields.Rows.Add(sourceField, fieldType, units);
            buttonUnlink.Enabled = true;
        }

        /// <summary>
        /// Handles the Click event for the the unlink button.
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonUnlink_Click(object sender, EventArgs e)
        {
            if (dataGridViewLinkedFields.Rows.Count <= 1)
            {
                return;
            }

            foreach (DataGridViewRow row in dataGridViewLinkedFields.SelectedRows) 
            {
                DataGridViewRow rowToRemove = row;
                dataGridViewLinkedFields.Rows.Remove(rowToRemove);
            }

            if (dataGridViewLinkedFields.Rows.Count <= 1)
            {
                buttonUnlink.Enabled = false;
            }
        }

        /// <summary>
        /// Handles the Click event for the the clear button.
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxOutputDatabase.Text = string.Empty;
            textBoxOutputTable.Text = string.Empty;
            listBoxInputs.Items.Clear();
            buttonLink.Enabled = false;
            buttonUnlink.Enabled = false;
            buttonImport.Enabled = false;
            dataGridViewLinkedFields.Rows.Clear();
        }

        /// <summary>
        /// Handles the Click event for the the help button.
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonHelp_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event for the the data output browse button
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            NewTableDialog newTableDialog = new NewTableDialog(this, false);
            newTableDialog.ShowDialog();
        }

        /// <summary>
        /// Handles the Click event for the the Import button
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void buttonImport_Click(object sender, EventArgs e)
        {            
            DataGridViewRowCollection rows = dataGridViewLinkedFields.Rows;
            //MessageBox.Show(rows[0].Cells[0].Value.ToString());
            //return;    
            bool success = false;
            success = NutriDatabase.ImportTable(inputPatient.Database, inputPatient.Table, outputPatient.Database,
                textBoxOutputTable.Text, rows, importConfig);

            if (success)
            {
                MessageBox.Show("The import was successful.");
                this.Close();
            }            
        }

        /// <summary>
        /// Handles the Load event for this dialog
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void ImportTableDialog_Load(object sender, EventArgs e)
        {
            OpenTableDialog openTableDialog = new OpenTableDialog(this);
            openTableDialog.ShowDialog(this);
            ConfigureFormControls();
        }

        /// <summary>
        /// Handles the KeyPress event for the output table name
        /// </summary>
        /// <param name="sender">Object that fired the event</param>
        /// <param name="e">.NET supplied event parameters</param>
        private void textBoxOutputTable_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            string keyInput = e.KeyChar.ToString();

            if (Char.IsLetterOrDigit(e.KeyChar) || Char.IsControl(e.KeyChar))
            {
                // Digits and letters are okay
            }
            else
            {
                // Consume this invalid key
                e.Handled = true;
            }
        }
        #endregion Events

        #region Properties
        #region Public Properties
        #endregion Public Properties

        #region Protected Properties
        #endregion Protected Properties

        #region Private Properties
        /// <summary>
        /// Gets/sets the name of the input file
        /// </summary>
        private bool Empty
        {
            get
            {
                return empty;
            }
            set
            {
                empty = value;
            }
        }

        /// <summary>
        /// Gets/sets the name of the input file
        /// </summary>
        private string InputFileName
        {
            get
            {
                return inputFileName;
            }
            set
            {
                inputFileName = value;
            }
        }

        /// <summary>
        /// Gets/sets the name of the output file
        /// </summary>
        private string OutputFileName
        {
            get
            {
                return outputFileName;
            }
            set
            {
                outputFileName = value;
            }
        }
        #endregion Private Properties
        #endregion Properties

        #region Methods
        #region Public Methods
        #endregion Public Methods

        #region Protected Methods
        /// <summary>
        /// Configures the form controls based on the user's preferences, as defined in the options. This
        /// includes loading language-specific strings.
        /// </summary>
        protected internal override NutriPatient GuiMediator()
        {
            if (Empty)
            {
                return inputPatient;
            }
            else
            {
                return outputPatient;
            }
        }

        /// <summary>
        /// Configures the form controls based on the user's preferences, as defined in the options,
        /// and loads any language translations.
        /// </summary>
        protected internal override void ConfigureFormControls()
        {
            this.Text = SharedStrings.IMPORT_TABLE;
            labelInputDatabase.Text = SharedStrings.INPUT_DATABASE;
            labelInputTable.Text = SharedStrings.INPUT_TABLE;
            labelOutputDatabase.Text = SharedStrings.OUTPUT_DATABASE;
            labelOutputTable.Text = SharedStrings.OUTPUT_TABLE;
            groupBoxSelectFields.Text = SharedStrings.SELECT_FIELDS;
            labelAs.Text = SharedStrings.AS;
            labelInputField.Text = SharedStrings.INPUT_FIELD;
            labelFieldType.Text = SharedStrings.FIELD_TYPE;
            labelLinkedFields.Text = SharedStrings.LINKED_FIELDS;

            buttonBrowse.Text = SharedStrings.ELIPSIS;
            buttonImport.Text = SharedStrings.IMPORT;
            buttonCancel.Text = SharedStrings.CANCEL;
            buttonClear.Text = SharedStrings.CLEAR;
            buttonHelp.Text = SharedStrings.HELP;
            buttonLink.Text = SharedStrings.LINK;
            buttonUnlink.Text = SharedStrings.UNLINK;
            
            buttonLink.Enabled = false;
            buttonUnlink.Enabled = false;
            buttonImport.Enabled = true;

            listBoxOutputs.Items.Clear();
            listBoxOutputs.Items.Add("Sex");
            listBoxOutputs.Items.Add("First name");
            listBoxOutputs.Items.Add("Last name");
            listBoxOutputs.Items.Add("Birth date");
            listBoxOutputs.Items.Add("ID number");

            listBoxOutputs.Items.Add("Date of measurement");
            listBoxOutputs.Items.Add("Age");
            listBoxOutputs.Items.Add("Height");
            listBoxOutputs.Items.Add("Weight");
            listBoxOutputs.Items.Add("Head circumference");
            listBoxOutputs.Items.Add("Subscapular skin fold");
            listBoxOutputs.Items.Add("Triceps skin fold");
            listBoxOutputs.Items.Add("Arm circumference");            

            listBoxOutputs.Items.Add("Edema");            
            listBoxOutputs.Items.Add("Recumbent");
            listBoxOutputs.Items.Add("Notes");

            //dataGridViewLinkedFields.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            listBoxInputs.Enabled = false;
            listBoxOutputs.Enabled = false;
            
        }

        /// <summary>
        /// Opens a database and data table for viewing and updating.
        /// </summary>
        protected internal override void OpenDatabase()
        {
            if (Empty)
            {
                textBoxInputDatabase.Text = inputPatient.ConnectionString;
                textBoxInputTable.Text = inputPatient.Table.TableName;                               
                Empty = false;                

                listBoxInputs.Items.Clear();
                
                for (int i = 0; i < inputPatient.Table.Columns.Count; i++)
                {
                    listBoxInputs.Items.Add(inputPatient.Table.Columns[i].ColumnName);
                }
            }
            else
            {                
                textBoxOutputDatabase.Text = outputPatient.ConnectionString;
                textBoxOutputTable.Text = outputPatient.Table.TableName;
                buttonLink.Enabled = true;
                listBoxInputs.Enabled = true;
                listBoxOutputs.Enabled = true;                
            }
        }
        #endregion Protected Methods

        #region Private Methods        
        #endregion Private Methods
        #endregion Methods
    }
}
